Search code examples
javascriptjquerypluginsw2ui

Modifing w2ui plugin


I am using w2ui(http://w2ui.com/) plugin.

Consider this example: 1

  1. I want when the add button is clicked an empty editable row be appeared in the grid to add a new row?

  2. I did not find a config to hide search box, how can I hide it?

  3. How can I set a column as id? currently it just recognize recid.

Solution

  • Here is a sample how to add new records in the grid when a button is clicked:

        <html>
        <head>
        <link rel="stylesheet" type="text/css" media="screen" href="../css/w2ui.css" /> 
        <script type="text/javascript" src="../js/jquery.min.js"></script>
        <script type="text/javascript" src="../js/w2ui.js"></script>
        <script>
        $(function () {
          $('#grid').w2grid({ 
            name: 'grid', 
            show: { 
              toolbar: true,
              footer: true,
              header: true,
              columnHeaders: true,
              lineNumbers: true,
              toolbarDelete: true,
              toolbarSave: true,
              toolbarAdd: true
            },
            columns: [        
              { field: 'recid', caption: 'ID', size: '50px', sortable: true, resizable: true, searchable: 'int' },
              { field: 'lname', caption: 'Last Name', size: '30%', sortable: true, resizable: true, searchable: true,
                editable: { type: 'text' }
              },
              { field: 'fname', caption: 'First Name', size: '30%', sortable: true, resizable: true, searchable: true,
                editable: { type: 'text' }
              },
            ],
            onAdd: function (target, data) {
              var recid = 1;
              if (this.records.length > 0) recid = (Math.max.apply(Math, this.find({}))) + 1;
              this.add({ recid: recid });
              $('#grid_grid_edit_'+ (this.records.length - 1) +'_1').focus();
            },
            onSave: function (target, data) {
              var obj = this;
              console.log(data);
              data.onComplete = function () {
                for (var r in data.changed) {
                  obj.get(data.changed[r].recid).editable = false;
                }
                obj.refresh();
              }
            }
          });
        });
        </script>
        </head>
        <body>
          <div id="grid" style="width: 100%; height: 500px;"></div>
        </body>
        </html>